#include "head.h"

#include "log.h"
#include "config.h"

#define CONFIG_LINE_SIZE 128
#define CONFIG_BUF_SIZE 4096

/*根据key值获取配置文件中的value值*/
int getParameter(const char *key, char *value){
    /*打开文件*/
    FILE *fp = fopen("config.ini", "r");
    /*错误检测*/
    if(fp == NULL){
        //logError("fopen faild in getparameter\n.");
        perror("fopen faild in getparameter.\n");
        exit(1);
    }

    /*循环查找，每次读取一行*/
    while(1){
        char line[CONFIG_LINE_SIZE];
        memset(line, 0, sizeof(line));
        /*读一行数据*/
        char *res = fgets(line, sizeof(line), fp);
        if(res == NULL){
            char buf[] = "没有要找的内容 \n";
            memcpy(value, buf, strlen(buf));
            return -1;
        }
        /*处理数据*/
        char *line_key = strtok(line, "=");
        if(strcmp(key, line_key) == 0){
            /*要找的内容*/
            char *line_value = strtok(NULL, "=");
            memcpy(value, line_value, strlen(line_value));
            fclose(fp);
            return 0;
        }
    }
}

/*设置key值和value值*/
int setParameter(const char *key, const char *value){
    /*打开文件*/
    FILE *fp = fopen("config.ini", "a+");
    /*错误检测*/
    if(!fp){
        printf("fopen faild in getparameter.\n");
        exit(1);
    }
    /*倒带*/
    rewind(fp);
    /*循环查找，每次读取一行*/
    while(1){
        char line[CONFIG_LINE_SIZE];
        memset(line, 0,  sizeof(line));
        /*读一行数据*/
        char *res = fgets(line, sizeof(line), fp);
        if(res == NULL){
            char buf[CONFIG_LINE_SIZE];
            memset(buf, 0, sizeof(buf));
            
            sprintf(buf, "%s=%s\n", key, value);
            int ret = fwrite(buf, strlen(buf), sizeof(char), fp);
            if(ret == -1){
                perror("fwrite: ");
                exit(1);
            }
            
            fclose(fp);
            return 0;
        }

        /*处理数据*/
        int line_len = strlen(line);
        char *line_key = strtok(line, "=");
        if(strcmp(key, line_key) == 0){
            /*找到key值*/
            char *line_value = strtok(NULL, "=");
            if(strcmp(value, line_value) == 0){
                /*修改值和原值相同*/
                fclose(fp);
                return 0;
            }else{
                /*修改值和原值不同*/
                /*记录所在行前后的文件流指针*/
                long offset_after = ftell(fp);
                fseek(fp, -line_len, SEEK_CUR);
                long offset_before = ftell(fp);
                /*倒带*/
                rewind(fp);

                /*写入新文件*/
                FILE *fp_new = fopen("new_config.ini", "w+");
                /*错误检测*/
                if(fp == NULL){
                    printf("fopen faild in getparameter.\n");
                    exit(1);
                }     
    
                /*创建缓冲数组，并初始化*/
                char buf[CONFIG_BUF_SIZE];
                memset(buf, 0, sizeof(buf));

                /*读源数据*/
                //int count = fread(buf, offset_before, sizeof(char), fp);
                int count = fread(buf, sizeof(char), offset_before, fp);
                if(count == -1){
                    printf("fread failed at fread in setParameter\n");
                    exit(1);
                }

                /*写入新文件*/
                fwrite(buf, count, sizeof(char), fp_new);
                fprintf(fp_new, "%s=%s\n", key, value);
                
                /*移动文件流指针*/
                fseek(fp, offset_after, SEEK_SET);
                /*清空buf*/
                memset(buf, 0, sizeof(buf));

                /*读源数据*/
                count = fread(buf, sizeof(buf), sizeof(char), fp);
                if(count == -1){                             
                    printf("fread failed at fread in setParameter\n");
                    exit(1);                                 
                }                                            
                
                /*写入新文件*/
                fwrite(buf, count, sizeof(char), fp_new);
                
                fclose(fp);
                fclose(fp_new);

                /*覆盖源文件*/
                system("mv new_config.ini config.ini");
                wait(NULL);
            }
            return 0;
        }
    }// !while, 
}
